﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Royalsoft.Common;

namespace NJL.WebUsers.AdminVisitor
{
    public partial class EditInfoCenter :BasePage
    {
        NJL.BLL.NJL_NEWS bllNews = new BLL.NJL_NEWS();
        NJL.Model.NJL_NEWS modelNews;
        NJL.BLL.NJL_CS_NEWS_TYPE bllNewstype = new BLL.NJL_CS_NEWS_TYPE();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               bind();
            }
        }
        void bind()
        {

            List<Model.NJL_NEWS> modelList=bllNews.GetModelList("NJL_PERSON_ID='"+UserId+"' and ID='"+Request.QueryString["id"]+"'");
            if (modelList.Count == 0)
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "请登录后再修改", "/Login.aspx");
                return;
            }           
            DataSet ds = bllNewstype.GetList("");
            ddlType.DataSource = ds;
            ddlType.DataTextField = "TYPE_NAME";
            ddlType.DataValueField = "TYPE_CODE";
            ddlType.DataBind();
            ddlType.Items.Insert(0, new ListItem("请选择", "0"));
            modelNews = bllNews.GetModel(Request.QueryString["id"].ToString());
         
            if (modelNews != null)
            {
                txtAttractionsTitle.Text = modelNews.NEWS_TITLE;
                txtAttractionFtitle.Text = modelNews.NEWS_SUBJECT_TITLE;
                txtTag.Text = modelNews.NEWS_TAG;
                txtContent.Text = modelNews.NEWS_CONTECT;
                ddlType.Items.FindByValue(modelNews.TYPE_CODE).Selected = true;
                if (modelNews.IMAGE_CODE == "")
                {
                    Image1.Visible = false;
                }
                else
                {
                    Image1.ImageUrl = GetImageUrl(modelNews.IMAGE_CODE);
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            modelNews = bllNews.GetModel(Request.QueryString["id"].ToString());
            modelNews.TYPE_CODE = ddlType.SelectedValue;
            modelNews.NJL_SYS_BASE_ID = WebSiteCode;
            modelNews.NEWS_TITLE = PageValidate.InputText(txtAttractionsTitle.Text,50);
            modelNews.NEWS_SUBJECT_TITLE = PageValidate.InputText(txtAttractionFtitle.Text,50);
            modelNews.NEWS_TAG = PageValidate.InputText(txtTag.Text,100);
            modelNews.NEWS_CONTECT = txtContent.Text;
            modelNews.UPDATE_DATE_TIME = DateTime.Now;
            modelNews.UPDATE_USER_ID = UserId;
            modelNews.UPDATE_USER_NAME = UserName;
            if (bllNews.Update(modelNews))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "InfoCenter.aspx");
            }
            
        }
        protected void btnUpload_Click1(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                modelNews = bllNews.GetModel(Request.QueryString["id"].ToString());
                if (modelNews != null)
                {
                    DeleteImageByCode(modelNews.IMAGE_CODE);   //删除文件
                    modelNews.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bllNews.Update(modelNews))
                    {
                       
                        if (modelNews.IMAGE_CODE != "")
                        {
                            Image1.ImageUrl = GetImageUrl(modelNews.IMAGE_CODE);
                            Image1.Visible = true;
                        }
                        else
                        {
                            Image1.Visible = false;
                        }
                        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "上传成功", "InfoCenter.aspx");
                    }

                }
            }
        }
    }
}